<?php
/**
 * Created by PhpStorm.
 * User: 钊鑫
 * Date: 2019/2/19
 * Time: 11:11
 */
namespace app\api\controller;

use Zzx\user\wechat\WxMethod;
use think\Controller;
use think\facade\Cache;

class WechatUser extends Controller
{
    private $config = [];    //微信公众号配置

    public function __construct($config = null)
    {
        parent::__construct();
//        if ($config === null) {
//            $config = Db::name('wx_user')->find();
//        }
        $config['appid'] = 'wx7c29ba1e2db9305f';
        $config['appsecret'] = 'a20c14d68ac706b11ed8dfa67db6625e';
        $this->WxMethod = new WxMethod($config['appid'],$config['appsecret']);
        $this->config = $config;
    }

    /*
     * 获取Token
     */
    public function getAccessToken()
    {
        $this->WxMethod->getAccessToken();
    }

    /**
     * 获取粉丝id列表
     * @param string $next_openid 下一次拉取的起始id的前一个id
     * @return array|bool
     */
    public function getFanIdList($next_openid='')
    {
        dump($this->WxMethod->getUserList());
    }

    /*
     * 获取粉丝详细信息
     * @param string $openid
     * @param string $access_token 如果为null，自动获取
     * @return array|bool
     */
    public function getFanInfo()
    {
        dump($this->WxMethod->getUserInfo('oJIU40sj7_lB35DDJPsBfz4qHNrY'));
    }


    /*
     * 向一个粉丝发送消息
     * 文档：https://mp.weixin.qq.com/wiki?action=doc&id=mp1421140547#2
     * @param $type string (text,news,image,voice,video,music,mpnews,wxcard)
     */
    public function sendMsgToOne()
    {
        $this->WxMethod->sendMsgToOne('oJIU40sj7_lB35DDJPsBfz4qHNrY','text','我发了');
    }

    /*
     * 获取用户所有模板消息
     * @return bool|mixed|string
     */
    public function getAllTemplateMsg()
    {
        dump($this->WxMethod->getAllTemplateMsg());
    }

    public function sendTemplateMsg($openid='oJIU40sj7_lB35DDJPsBfz4qHNrY', $template_id='qRVUP2QptyM2RAG8OuFujjtaF_MdtV1cq-rvW6FcPzk', $url='', $data='')
    {
        $data = [
            "project" => [
                "value" => "项目！",
                "color" => "#173177"
            ],
            "username" => [
                "value" => "项目负责人",
                "color" => "#173177"
            ],
            "type" => [
                "value" => "审核方！",
                "color" => "#173177"
            ],
            "result" => [
                "value" => "审核结果！",
                "color" => "#173177"
            ],
            "time" => [
                "value" => "审核时间！",
                "color" => "#173177"
            ],
            "content" => [
                "value" => "审核内容！",
                "color" => "#173177"
            ]
        ];
        $this->WxMethod->sendTemplateMsg('oJIU40sj7_lB35DDJPsBfz4qHNrY','qRVUP2QptyM2RAG8OuFujjtaF_MdtV1cq-rvW6FcPzk','',$data);
    }

    /*
    * 获取微信JS-SDK的config权限配置
    * @return bool|mixed|string
    */
    public function getSignPackage()
    {
        Cache::clear();
        $getSignPackage = $this->WxMethod->getSignPackage();
        $this->assign('getSignPackage',$getSignPackage);
        return $this->fetch();
    }
}